package ru.dc.gui.swing;

import org.jdesktop.swingx.JXTable;
import ru.dc.Application;
import ru.dc.object.FavouriteHub;
import ru.dc.object.FavouriteHubListener;
import ru.dc.object.events.FavouriteHubEvent;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * <pre>
 * User: Penkov Vladimir
 * Date: 08.11.2010
 * Time: 17:40:20
 * </pre>
 */
public class FavouriteHubs extends Tab implements FavouriteHubListener {
    private JPanel rootPanel;
    private JPanel wrapper;
    private JButton createBtn;
    private JButton removeBtn;
    private JButton editBtn;
    private JXTable hubs;

    public FavouriteHubs() {


        setLayout(new BorderLayout());
        add(rootPanel, BorderLayout.CENTER);


        hubs.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                int selectedCount = hubs.getSelectedRowCount();
                boolean any = selectedCount > 0;
                removeBtn.setEnabled(any);
                editBtn.setEnabled(any);
            }
        });



        final FavouritesTableModel favouritesTableModel = new FavouritesTableModel();
        favouritesTableModel.addFavouriteHubs(Application.getApplication().getFavouritesService().getFavouriteHubs());
        hubs.setModel(favouritesTableModel);
        hubs.setSortOrder(0, SortOrder.ASCENDING);
        hubs.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {

                if (e.getClickCount()==2 && SwingUtilities.isLeftMouseButton(e)) {
                    MainFrame mainFrame = Application.getApplication().getMainFrame();
                    int viewRow = hubs.getSelectedRow();
                    int modelRow = hubs.convertRowIndexToModel(viewRow);
                    FavouriteHub hub = favouritesTableModel.getFavouriteHub(modelRow);

                    if (mainFrame.containsTab(hub.getServerInfo())) {
                        mainFrame.selectTab(hub.getServerInfo());
                    }
                    else {
                        mainFrame.addHubConnectionTab(hub.getServerInfo());
                    }

                }
            }
        });


        createBtn.addActionListener(new AbstractAction() {

            public void actionPerformed(ActionEvent e) {
                FavouriteHubEditDlg dlg = new FavouriteHubEditDlg(Application.getApplication().getMainFrame());
                if (dlg.execute()==JOptionPane.OK_OPTION) {

                    FavouriteHub hub = new FavouriteHub();
                    hub.setName(dlg.getName());
                    hub.setAutoConnect(dlg.isAutoconnect());
                    hub.getServerInfo().setEncoding(dlg.getEncoding());
                    hub.getServerInfo().setNick(dlg.getNick());
                    String[] addr_port = dlg.getAddress().split(":");
                    if (addr_port.length>=2) {
                        hub.getServerInfo().setAddress(addr_port[0]);
                        hub.getServerInfo().setPort(new Integer(addr_port[1]));
                    }
                    else {
                        hub.getServerInfo().setAddress(addr_port[0]);
                        hub.getServerInfo().setPort(411);
                    }
                    Application.getApplication().getFavouritesService().addFavouriteHub(hub);
                }
            }
        });


        editBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                int viewRow = hubs.getSelectedRow();
                int modelRow = hubs.convertRowIndexToModel(viewRow);
                FavouriteHub hub = favouritesTableModel.getFavouriteHub(modelRow);

                FavouriteHubEditDlg dlg = new FavouriteHubEditDlg(Application.getApplication().getMainFrame());
                dlg.setAddress(hub.getServerInfo().getAddress()+":"+hub.getServerInfo().getPort());
                dlg.setEncoding(hub.getServerInfo().getEncoding());
                dlg.setAutoconnect(hub.isAutoConnect());
                dlg.setName(hub.getName());
                dlg.setNick(hub.getServerInfo().getNick());

                if (dlg.execute()==JOptionPane.OK_OPTION) {

                    Application.getApplication().getFavouritesService().removeFavouriteHub(hub);
                    hub = new FavouriteHub();
                    hub.setName(dlg.getName());
                    hub.setAutoConnect(dlg.isAutoconnect());
                    hub.getServerInfo().setEncoding(dlg.getEncoding());
                    hub.getServerInfo().setNick(dlg.getNick());
                    String[] addr_port = dlg.getAddress().split(":");
                    if (addr_port.length>=2) {
                        hub.getServerInfo().setAddress(addr_port[0]);
                        hub.getServerInfo().setPort(new Integer(addr_port[1]));
                    }
                    else {
                        hub.getServerInfo().setAddress(addr_port[0]);
                        hub.getServerInfo().setPort(411);
                    }
                    Application.getApplication().getFavouritesService().addFavouriteHub(hub);
                }


            }
        });


        removeBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int viewRow = hubs.getSelectedRow();
                int modelRow = hubs.convertRowIndexToModel(viewRow);
                FavouriteHub hub = favouritesTableModel.getFavouriteHub(modelRow);
                Application.getApplication().getFavouritesService().removeFavouriteHub(hub);
            }
        });


    }

    @Override
    public void init() {
        super.init();
        setTitle("Избранные хабы");
        Application.getApplication().getFavouritesService().addFavouriteHubListener(this);
    }

    @Override
    public void close() {
        Application.getApplication().getFavouritesService().removeFavouriteHubListener(this);
    }

    public void onEvent(final FavouriteHubEvent event) {
        final FavouritesTableModel favouritesTableModel = (FavouritesTableModel) hubs.getModel();
        switch (event.getOperation()) {
            case FavouriteHubEvent.FAVOURITE_HUB_ADDED: {
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        favouritesTableModel.addFavouriteHub(event.getSource());
                    }
                });
                break;
            }
            case FavouriteHubEvent.FAVOURITE_HUB_REMOVED: {
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        favouritesTableModel.removeFavouriteHub(event.getSource());
                    }
                });
                break;
            }
        }
    }
}
